
package com.gzhryc.shared_device.oem.h5.mng.controllers.device;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.DeviceTypeVersion;
import com.gzhryc.shared_device.common.device.services.DeviceTypeVersionService;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class DeviceTypeVersionController {

    @Route(value = "/device/type/version/list.data")
    public ListResult<DeviceTypeVersion> listData(HttpServletRequest request) {
        ListResult<DeviceTypeVersion> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        String typeId = WebUtils.getString(request,"typeId");
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(DeviceTypeVersionService.self().countByTypeId(typeId));
        result.getBody().setData(DeviceTypeVersionService.self().findByTypeId(typeId,pageNo,pageLimit));
        if (result.getBody().getData() != null && result.getBody().getData().size() > 0) {
            result.setCode(DtoConstants.SUCCESS,null);
        }
        return result;
    }

    @Route(value = { "/device/type/version/add.html" })
    public String add(HttpServletRequest request) {
        return "/device/type/version/edit.html";
    }

    @Route(value = { "/device/type/version/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"新增失败，请稍后再试");
        DeviceTypeVersion deviceTypeVersion = WebUtils.getParameter(request,DeviceTypeVersion.class);
        try {
            if(DeviceTypeVersionService.self().add(deviceTypeVersion)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/version/update.html" })
    public String update(HttpServletRequest request) {
        String typeId = WebUtils.getString(request,"typeId");
        String versionNumber = WebUtils.getString(request,"versionNumber");
        DeviceTypeVersion deviceTypeVersion = DeviceTypeVersionService.self().getByKeys(typeId,versionNumber);
        WebUtils.setAttribute(request,deviceTypeVersion);
        return "/device/type/version/edit.html";
    }

    @Route(value = { "/device/type/version/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"修改失败，请稍后再试");
        DeviceTypeVersion deviceTypeVersion = WebUtils.getParameter(request,DeviceTypeVersion.class);
        try {
            if(DeviceTypeVersionService.self().updateNotNull(deviceTypeVersion)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Route(value = { "/device/type/version/del.do" })
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"删除失败，请稍后再试");
        String typeId = WebUtils.getString(request,"typeId");
        String versionNumber = WebUtils.getString(request,"versionNumber");
        try {
            if(DeviceTypeVersionService.self().deleteByKeys(typeId,versionNumber)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
